﻿using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.UI;

/// <summary>
///   <para>Adds an outline to a graphic using IVertexModifier.</para>
/// </summary>
[AddComponentMenu("UGUI/Effects/Outline")]
public class UGUIOutline : UGUIShadow
{
    [SerializeField]
    public bool diagonal = true;

    [SerializeField]
    public bool highQualityOutline = false;

    protected UGUIOutline()
    {
    }

    public override void ModifyMesh(VertexHelper vh)
    {
        if (!this.IsActive()) { return; }

        List<UIVertex> list = UGUIListPool<UIVertex>.Get();
        vh.GetUIVertexStream(list);

        int num = list.Count * 5;
        if (list.Capacity < num)
        {
            list.Capacity = num;
        }

        int start;
        int count = 0;

        if (diagonal || highQualityOutline)
        {
            start = 0;
            count = list.Count;
            base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, +base.EffectDistance.x, +base.EffectDistance.y);

            start = count;
            count = list.Count;
            base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, +base.EffectDistance.x, -base.EffectDistance.y);

            start = count;
            count = list.Count;
            base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, -base.EffectDistance.x, +base.EffectDistance.y);

            start = count;
            count = list.Count;
            base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, -base.EffectDistance.x, -base.EffectDistance.y);
        }

        if (!diagonal || highQualityOutline)
        {
            start = count;
            count = list.Count;
            base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, 0, -base.EffectDistance.y);

            start = count;
            count = list.Count;
            base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, 0, +base.EffectDistance.y);

            start = count;
            count = list.Count;
            base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, -base.EffectDistance.x, 0);

            start = count;
            count = list.Count;
            base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, +base.EffectDistance.x, 0);
        }

        vh.Clear();
        vh.AddUIVertexTriangleStream(list);

        UGUIListPool<UIVertex>.Release(list);
    }
}